package com.xquant.transaction_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xquant.transaction_management_system.model.PO.*;
import com.xquant.transaction_management_system.service.FinancialInstrumentsService;
import com.xquant.transaction_management_system.service.Impl.FinancialInstrumentsServiceImpl;
import com.xquant.transaction_management_system.service.PersonalConfigService;
import com.xquant.transaction_management_system.service.TransactionControlConfigurationService;
import com.xquant.transaction_management_system.service.TransactionService;
import com.xquant.transaction_management_system.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("金融控制配置控制层")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class TransactionControlConfigurationController {
    @Autowired
    private PersonalConfigService personalConfigService;
    @Autowired
    private TransactionControlConfigurationService transactionControlConfigurationService;
    @Autowired
    private TransactionService transactionService;
    @Autowired
    private FinancialInstrumentsService financialInstrumentsService;
    @PostMapping("/saveConfiguration")
    public Result saveConfiguration(@RequestBody TransactionControlConfiguration transactionControlConfiguration){
        transactionControlConfigurationService.save(transactionControlConfiguration);
        Long id = transactionControlConfiguration.getId();
        return Result.ok(id);
    }
    @GetMapping("/getConfig/{Id}")
    public Result saveConfiguration(@PathVariable Long Id){
        TransactionControlConfiguration byId = transactionControlConfigurationService.getById(Id);
        return Result.ok(byId);
    }
    @PostMapping("/saveConfigurationOver")
    public Result updateConfiguration(@RequestBody TransactionControlConfiguration transactionControlConfiguration){
        transactionControlConfigurationService.updateById(transactionControlConfiguration);
        transactionControlConfiguration.getPriceDeviation();
        Long configurationId = transactionControlConfiguration.getId();
        String account = transactionControlConfiguration.getTransactionAccount();
        List<FinancialInstruments> byConfigID = financialInstrumentsService.getByConfigID(configurationId);
        for (FinancialInstruments financialInstruments : byConfigID) {
            PersonalConfig personalConfig = new PersonalConfig();
            personalConfig.setConfigurationId(configurationId);
            personalConfig.setTransactionAccount(account);
            personalConfig.setPriceDeviation(transactionControlConfiguration.getPriceDeviation());
            personalConfig.setTransactionDirection(transactionControlConfiguration.getTransactionDirection());
            personalConfig.setDebentureCode(financialInstruments.getDebentureCode());
            personalConfigService.save(personalConfig);
            financialInstruments.setCongigurationId(0L);
            financialInstrumentsService.updateById(financialInstruments);
        }
        List<PersonalConfig> list = personalConfigService.getByAccount(account);
        for (PersonalConfig personalConfig : list) {
            transactionService.getByAccountAndDecode(personalConfig.getTransactionAccount(),personalConfig.getDebentureCode(),transactionControlConfiguration.getPriceDeviation());
        }
        return Result.ok();
    }
}
